/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.azet;

import java.util.Date;
import org.apache.log4j.Logger;
import sk.yw.azetclient.model.AbstractMessage;
import sk.yw.azetclient.model.Message;
import sk.yw.azetclient.model.MessageBean;
import sk.yw.azetclient.model.MessageThread;

/**
 * This class represents single RP.
 *
 * @author error216
 */
public class AzetMessage extends AbstractMessage {
    
    private static final Logger logger = Logger.getLogger(AzetMessage.class);
    
    protected String sign;
    protected Date timeOfPrevious;
    protected String textOfPrevious;
    protected volatile String lastFragment;
    
    public AzetMessage(MessageBean messageBean, MessageThread thread) {
        super(messageBean, thread);
        
        if (messageBean instanceof AzetMessageBean) {
            AzetMessageBean azetMessageBean = (AzetMessageBean) messageBean;
            sign = azetMessageBean.getSign();
            
            Date beanTimeOfPrevious = azetMessageBean.getTimeOfPrevious();
            if (beanTimeOfPrevious == null) {
                timeOfPrevious = null;
            } else {
                timeOfPrevious = (Date) beanTimeOfPrevious.clone();
            }
            
            textOfPrevious = azetMessageBean.getTextOfPrevious();
            lastFragment = azetMessageBean.getLastFragment();
        } else {
            throw new UnsupportedOperationException("Unsupported MessageBean type.");
        }
    }
    
    public String getSign() {
        return sign;
    }

    public Date getTimeOfPrevious() {
        if (timeOfPrevious == null) {
            return null;
        } else {
            return (Date) timeOfPrevious.clone();
        }
    }

    public String getTextOfPrevious() {
        return textOfPrevious;
    }

    public synchronized String getLastFragment() {
        return lastFragment;
    }

    public synchronized void setLastFragment(String lastFragment) {
        this.lastFragment = lastFragment;
        thread.setStored(false);
        logger.debug("Last fragment set to: " + lastFragment);
    }
    
    public boolean isReplyTo(Message message) {
        if (sender == null
                || receiver == null
                || textOfPrevious == null) {
            return false;
        }
        if (message instanceof AzetMessage) {
            AzetMessage aMessage = (AzetMessage) message;
            return sender.equals(aMessage.getReceiver())
                && receiver.equals(aMessage.getSender())
                && ( (aMessage.getLastFragment() != null
                && textOfPrevious.equals(aMessage.getLastFragment()))
                || textOfPrevious.equals(aMessage.getContent()) );
        } else {
            return false;
        }
        
    }
    
    public AzetMessageBean createReplyBean() {
        AzetMessageBean newMessage = new AzetMessageBean();
        newMessage.setSender(receiver);
        newMessage.setReceiver(sender);
        newMessage.setTimeOfPrevious(sendTime);
        if (getLastFragment() != null) {
            newMessage.setTextOfPrevious(getLastFragment());
        } else {
            newMessage.setTextOfPrevious(content);
        }
        newMessage.setSign(sign);
        
        return newMessage;
    }
    
    public AzetMessageBean createPossiblePreviousBean() {
        AzetMessageBean previous = new AzetMessageBean();
        previous.setSender(receiver);
        previous.setReceiver(sender);
        previous.setSendTime(timeOfPrevious);
        previous.setContent(textOfPrevious);
        
        return previous;
    }
    
    public AzetMessageBean createMutable() {
        return new AzetMessageBean(this);
    }
}
